package cn.com.headfree.expert.controller;

import cn.com.headfree.base.controller.ExBaseController;
import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.entity.*;
import cn.com.headfree.entity.TDeclareExpertInfo;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.expert.service.IDeclareExpertService;
import cn.com.headfree.util.ExcelExportUtil;
import com.gta.edu.sdk.common.dto.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author wcSun
 * @Date 2022/6/27
 **/
@Controller
@RequestMapping(value = "/declare/expert", produces = "application/json;charset=UTF-8")
public class DeclareExpertController extends ExBaseController {

    private static final Logger LOG = LogManager.getLogger(DeclareExpertController.class);

    @Autowired
    private IDeclareExpertService declareExpertService;

    /**
     * 保存
     */
    @RequestMapping("/saveDeclareExpertInfo")
    @ResponseBody
    public ResultData saveDeclareExpertInfo(HttpServletRequest request, TDeclareExpertInfo tDeclareExpertInfo) {
        ResultData rs = new ResultData();
        User user = this.getLoginUserInfo(request);
        try {
            tDeclareExpertInfo.setErpNo(user.getFax());
            tDeclareExpertInfo.setUserName(user.getName());
            List<TDeclareExpertInfo> declareExpertInfos = declareExpertService.judge(tDeclareExpertInfo);
            if (CollectionUtils.isNotEmpty(declareExpertInfos)) {
                rs.setSuccess(false);
                rs.setErrorMsg("当前年度已申请调整");
            } else {
                tDeclareExpertInfo.setErpNo(user.getFax());
                tDeclareExpertInfo.setUserName(user.getName());
                tDeclareExpertInfo.setOrgId(user.getBaseOrgId());
                tDeclareExpertInfo.setOrgName(user.getBaseOrgName());
                declareExpertService.saveDeclareExpertInfo(tDeclareExpertInfo);
                rs.setSuccess(true);
                LOG.info("保存成功");
            }
        } catch (Exception e) {
            rs.setSuccess(false);
            rs.setErrorMsg("保存失败！");
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 分页查询列表
     *
     * @param page
     * @param year
     * @return
     */
    @RequestMapping("/getPageDeclareExpertInfo")
    @ResponseBody
    public ResultData getPageDeclareExpertInfo(Page<TDeclareExpertInfo> page, String year, HttpServletRequest request) {
        ResultData rs = new ResultData();
        try {
            User user = this.getLoginUserInfo(request);
            declareExpertService.getPageDeclareExpertInfo(page, year, user);
            rs.setData(page);
            LOG.info("查询成功");
        } catch (Exception e) {
            rs.setSuccess(false);
            rs.setErrorMsg("查询失败！");
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 管理员审核同意：1、申报状态设为同意
     */
    @RequestMapping("/auditAgreeExpertDeclareInfo")
    @ResponseBody
    public ResultData auditAgreeExpertDeclareInfo(HttpServletRequest request,String declareId){
        ResultData result = new ResultData();
        try{
            User user = this.getLoginUserInfo(request);
            declareExpertService.auditAgreeInfo(declareId,user);
            result.setSuccess(true);
            result.setData("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
            result.setErrorMsg("操作失败");
        }
        return result;
    }

    /**
     * 管理员驳回：1、删除积分明细；2、申报状态设为不同意
     */
    @RequestMapping("/auditRejectExpertDeclareInfo")
    @ResponseBody
    public ResultData auditRejectExpertDeclarePointInfo(HttpServletRequest request,String declareId,String auditOpt){
        ResultData result = new ResultData();
        try{
            User user = this.getLoginUserInfo(request);
            declareExpertService.auditRejectInfo(declareId,user,auditOpt);
            result.setSuccess(true);
            result.setData("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setSuccess(false);
            result.setErrorMsg("操作失败");
        }
        return result;
    }

    /**
     * 分页查询列表:人才申报评定查询
     *
     * @param page
     * @return
     */
    @RequestMapping("/getPageDeclareExpertQuery")
    @ResponseBody
    public ResultData getPageDeclareExpertQuery(Page<TDeclareExpertInfo> page, TDeclareExpertInfo declareExpertInfo, HttpServletRequest request) {
        ResultData rs = new ResultData();
        try {
            User user = this.getLoginUserInfo(request);
            declareExpertService.getPageDeclareExpertInfoQuery(page, user.getBaseOrgId(), declareExpertInfo.getDeptId(),declareExpertInfo.getUserName(),declareExpertInfo.getErpNo(),declareExpertInfo.getYear(),declareExpertInfo.getAuditStatus());
            if (!page.getResult().isEmpty()) {
                page.getResult().forEach(p -> {
                    if (null != p.getYear() && p.getYear() == 0) {
                        p.setYear(null);
                    }
                });
            }
            rs.setData(page);
            LOG.info("查询成功");
        } catch (Exception e) {
            rs.setSuccess(false);
            rs.setErrorMsg("查询失败！");
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 人才评定申报查询导出报表
     */
    @GetMapping("/exportExcelOfDeclareExpertQuery")
    public void exportExcelOfDeclareExpertInfoQuery(HttpServletRequest request, HttpServletResponse response, TDeclareExpertInfo declareExpertInfo) {
        try {
            User user = this.getLoginUserInfo(request);
            //查询要导出的报表数据
            List<TDeclareExpertInfo> list = declareExpertService.getDeclareExpertInfoQuery(user.getBaseOrgId(), declareExpertInfo.getDeptId(),declareExpertInfo.getUserName(),declareExpertInfo.getErpNo(),declareExpertInfo.getYear(),declareExpertInfo.getAuditStatus());
            String personYearPointDetailHead = "ERP编号,姓名,申报年份,部门,申报状态";
            //获取标题
            String[] personYearPointDetailHeads = personYearPointDetailHead.split(",");
            //sheet名称
            String sheetName = "人才评定申报查询导出";
            //获取数据
            String[][] Content = ExcelExportUtil.declareExpertInfoQuerHead(list, personYearPointDetailHeads);
            //创建workbook
            XSSFWorkbook workbook = new XSSFWorkbook();
            workbook = ExcelExportUtil.exportExcel(workbook, personYearPointDetailHeads, Content, sheetName);//基本信息
            //响应生成excel
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
            LOG.info("开始导出excel报表");
            ExcelExportUtil.exportExcel2Browser(response, sheetName + sdf.format(new Date()), workbook);
            LOG.info("导出excel报表成功");
        } catch (Exception e) {
            LOG.error("导出失败");
            e.printStackTrace();
        }
    }
}
